﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Configuration;

namespace iPublic
{
    /// <summary>
    /// 模拟gs_class的部分功能
    /// 修改记录：
    ///   20171214，zch
    /// </summary>
    public partial class gsClass : Gs_Class { }
    public partial class Gs_Class
    {
        #region //getAppConfigValue，读取配置值
        public static object getAppConfigValue(string AConfigKeyName, object ADefault, string AConfigFileName = "")
        {
            string s = "", sFile = AConfigFileName;
            object result = ADefault, o = null;
            //if (!string.IsNullOrEmpty(sFile)) sFile = findSysFile(AConfigFileName); //指定了文件名
            //
            if (string.IsNullOrEmpty(AConfigFileName) || !System.IO.File.Exists(sFile))
            {   //文件名无效，直接读取系统内置的配置文件
                o = ConfigurationManager.AppSettings[AConfigKeyName];
            }
            else
            {   //文件名有效，则读取此文件。参考：http://www.cnblogs.com/zfanlong1314/p/3623622.html
                ExeConfigurationFileMap c = new ExeConfigurationFileMap();
                c.ExeConfigFilename = sFile;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(c, ConfigurationUserLevel.None);
                KeyValueConfigurationElement k = config.AppSettings.Settings[AConfigKeyName];
                if (k != null) o = k.Value;
                //释放
                c = null;
                config = null;
                k = null;
            }
            if (o != null) result = o;
            return result;
        }
        #endregion

        #region //getAppConfigNumber，读取config配置中AppSettings里的值，数字型，读取到0或者没有配置都会用默认值
        /// <summary>
        /// 读取Web.config中AppSettings里的值，数字型，读取到0或者没有配置都会用默认值
        /// </summary>
        /// <param name="AConfigKeyName">appSettings里的Key名字</param>
        /// <param name="ADefault">默认值</param>
        /// <returns>返回读取到的值</returns>
        public static double getAppConfigNumber(string AConfigKeyName, double ADefault, string AConfigFileName)
        {
            double result = ADefault, v = 0;
            object o = getAppConfigValue(AConfigKeyName, ADefault, AConfigFileName);
            if (o != null)
            {
                double d = DateTime.Now.ToOADate() - 1234.567;
                v = varToFloatDef(o, d);
                if (v != d) result = v;
            }
            return result;
        }
        public static double getAppConfigNumber(string AConfigKeyName, double ADefault) { return getAppConfigNumber(AConfigKeyName, ADefault, ""); }
        #endregion

        #region //getAppConfigStr，读取config配置中AppSettings里的值，字符型，读取到空或者没有配置都会用默认值
        /// <summary>
        /// 读取Web.config中AppSettings的值，字符型
        /// </summary>
        /// <param name="AConfigKeyName">AppSettings中的Key值</param>
        /// <param name="ADefault">默认值</param>
        /// <returns></returns>
        public static string getAppConfigStr(string AConfigKeyName, string ADefault, string AConfigFileName)
        {
            string result = ADefault, v = "";
            object o = getAppConfigValue(AConfigKeyName, ADefault, AConfigFileName);
            if (o != null)
            {
                v = o.ToString();
                if (!string.IsNullOrEmpty(v)) result = v;
            }
            return result;
        }
        public static string getAppConfigStr(string AConfigKeyName, string ADefault) { return getAppConfigStr(AConfigKeyName, ADefault, ""); }
        #endregion

        #region //getAppConfigDate，读取config配置中AppSettings里的值，日期型，读取到0或者没有配置都会用默认值
        /// <summary>
        /// 读取Web.config中AppSettings的值，字符型
        /// </summary>
        /// <param name="AConfigKeyName">AppSettings中的Key值</param>
        /// <param name="ADefault">默认值</param>
        /// <returns></returns>
        public static DateTime getAppConfigDate(string AConfigKeyName, DateTime ADefault, string AConfigFileName)
        {
            DateTime result = ADefault, v = DateTime.Today;
            object o = getAppConfigValue(AConfigKeyName, ADefault, AConfigFileName);
            if (o != null)
            {
                v = Convert.ToDateTime(o.ToString());
                if (v != null && v != DateTime.Today) result = v;
            }
            return result;
        }
        public static DateTime getAppConfigDate(string AConfigKeyName, DateTime ADefault) { return getAppConfigDate(AConfigKeyName, ADefault, ""); }
        #endregion

        #region //getAppConfigBool，读取config配置中AppSettings里的值，逻辑值
        /// <summary>
        /// 读取Web.config中AppSettings里的值，逻辑值，getAppConfigBool
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static bool getAppConfigBool(string AConfigKeyName, bool ADefault, string AConfigFileName)
        {
            bool result = ADefault;
            string s = getAppConfigStr(AConfigKeyName, "", AConfigFileName).Trim().ToUpper();
            if (result)
            {   //默认值倾向于true，则只有特定条件下才是false
                if (s == "FALSE" || s == "0") result = false;
            }
            else
            {   //默认值倾向于false，则只有特定条件下才是true
                if (s == "TRUE" || s == "1") result = true;
            }
            return result;
        }
        public static bool getAppConfigBool(string AConfigKeyName, bool ADefault) { return getAppConfigBool(AConfigKeyName, ADefault, ""); }
        #endregion



        #region//tryNullTo：强制转换默认值
        /// <summary>
        /// 强制转换默认值
        /// </summary>
        /// <param name="oValue"></param>
        /// <param name="oDefault"></param>
        /// <returns></returns>
        public static object tryNullTo(object oValue, object oDefault)
        {   //强制转换默认值
            try
            { if (oValue == null) return oDefault; else return oValue; }
            catch (Exception x)
            { return oDefault; }
        }
        public static string tryNullToString(object oValue, string oDefault)
        { return tryNullTo(oValue, oDefault).ToString(); }
        #endregion


        #region //varToIntDef：字符转成数字，失败返回默认值
        /// <summary>
        /// 字符转成数字Int整型，默认值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>转换成功则返回，失败则返回默认值</returns>
        public static int varToIntDef(object str, int vDefault)
        {   //
            int result = vDefault;
            if (str == null) return result;
            try
            { result = Convert.ToInt32(str); }
            catch (Exception x) { x = null; }
            //
            return result;
        }
        /// <summary>
        /// 字符转成数字Float，默认值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>转换成功则返回，失败则返回默认值</returns>
        public static double varToFloatDef(object str, double vDefault)
        {   //
            double result = vDefault;
            if (str == null) return result;
            try
            { result = Convert.ToDouble(str); }
            catch (Exception x) { }
            //
            return result;
        }
        /// <summary>
        /// 变量转换成string，默认值
        /// </summary>
        /// <param name="str">变量</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>返回结果</returns>
        public static string varToStringDef(object str, string vDefault)
        {   //
            string result = vDefault;
            if (str == null) return result;
            try
            { result = str.ToString(); }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        /// <summary>
        /// 变量转换成dateTime，默认值
        /// </summary>
        /// <param name="str">变量</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>返回结果</returns>
        public static DateTime varToDateTimeDef(object str, DateTime vDefault)
        {
            DateTime result = vDefault, d = vDefault;
            if (str == null) return result;
            //
            //
            try
            {
                //if (str is TextBox && DateTime.TryParse((str as TextBox).Text, out d)) result = d;  //Convert.ToDateTime((str as TextBox).Text);
                //else
                if (DateTime.TryParse(str.ToString(), out d)) result = d;  //Convert.ToDateTime(str);
            }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        /// <summary>
        /// 值强制转化为逻辑型，如果出错，返回默认值
        /// </summary>
        /// <param name="value">要处理的值</param>
        /// <param name="vDefault">默认值</param>
        /// <returns></returns>
        public static bool varToBoolDef(object value, bool vDefault)
        {
            bool result = vDefault;
            if (value == null) return result;
            //
            try
            { result = Convert.ToBoolean(value); }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        #endregion


        #region //getDefaultValueOfType：取类型的默认值
        /// <summary>
        /// 取类型的默认值，比default(T)好在便于控制
        /// </summary>
        /// <param name="type">要取的类型</param>
        /// <returns>返回默认值，object类型返回DBNull</returns>
        public static object getDefaultValueOfType(Type type)
        {   //取类型的默认值
            object result = null;
            if (isNumber(type)) result = 0;
            else if (type == typeof(System.Boolean)) result = false;
            else if (type == typeof(System.Byte)) result = 1;
            else if (type == typeof(System.SByte)) result = 1;
            else if (type == typeof(System.Char)) result = "";
            else if (type == typeof(System.String)) result = "";
            //其他类型
            else if (type == typeof(System.DateTime)) result = DateTime.Today;   //GsDefineTypes.dBlankDate
            //全部都不是，只是基类
            else if (type == typeof(System.Object)) result = null;
            //
            return result;
        }
        #endregion


        #region //是数字类型isNumber
        public static bool isNumber(Type type)
        {
            bool result = false;
            if (type == typeof(System.Decimal)) result = true;
            if (type == typeof(System.Double)) result = true;
            if (type == typeof(System.Single)) result = true;
            if (type == typeof(System.Int32)) result = true;
            if (type == typeof(System.UInt32)) result = true;
            if (type == typeof(System.Int64)) result = true;
            if (type == typeof(System.UInt64)) result = true;
            if (type == typeof(System.Int16)) result = true;
            if (type == typeof(System.UInt16)) result = true;
            return result;
        }
        #endregion

    }
}
